package archimede;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Scanner;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.UIManager;

import net.miginfocom.swing.MigLayout;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;


public class Intonaco extends JPanel {

   private String[] elIntonaco = { "", "calce", "cementizio", "base calce", "base cementizio", "macro poroso", "micro poroso", "non rilevabile",
         "non presente", "tradizionale", "idrorepellente", "altro" };
   private String[] elPittura = { "", "silicati", "silossanica", "a calce", "elastomerica", "graffiato", "acrilica", "base quarzo", "non presente ",
         "non rilevabile", "cappotto termico", "idropittura", "altro" };

   private JComboBox intonacoEsterno = new JComboBox(elIntonaco);
   private boolean modifyIntonEst = false;
   private JTextField otherIntonEst = new JTextField() {
      @Override
      protected void paintComponent(Graphics g) {
         if (!isOpaque()) {
            int w = getWidth();
            int h = getHeight();
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(UIManager.getColor("TextField.background"));
            g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
            g2.setColor(Color.BLACK);
            g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
            g2.dispose();
         }
         super.paintComponent(g);
      }
   };

   private JComboBox pitturaEsterno = new JComboBox(elPittura);
   private boolean modifyPittEst = false;
   private JTextField otherPittEst = new JTextField() {
      @Override
      protected void paintComponent(Graphics g) {
         if (!isOpaque()) {
            int w = getWidth();
            int h = getHeight();
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(UIManager.getColor("TextField.background"));
            g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
            g2.setColor(Color.BLACK);
            g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
            g2.dispose();
         }
         super.paintComponent(g);
      }
   };

   private JTextPane note = new JTextPane() {
      @Override
      protected void paintComponent(Graphics g) {
         if (!isOpaque()) {
            int w = getWidth();
            int h = getHeight();
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(UIManager.getColor("TextField.background"));
            g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
            g2.setColor(Color.BLACK);
            g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
            g2.dispose();
         }
         super.paintComponent(g);
      }
   };
   private JTextField annoLEst = new JTextField() {
      @Override
      protected void paintComponent(Graphics g) {
         if (!isOpaque()) {
            int w = getWidth();
            int h = getHeight();
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(UIManager.getColor("TextField.background"));
            g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
            g2.setColor(Color.BLACK);
            g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
            g2.dispose();
         }
         super.paintComponent(g);
      }
   };

   private StartingWindow st;

   private LatoIntonacoPanel latoIntonaco;

   public Intonaco(StartingWindow st) {
      super();
      this.st = st;
      latoIntonaco = new LatoIntonacoPanel(st);
      setOpaque(false);
      setLayout(new MigLayout());
      RoundedPanel lato = new RoundedPanel("Lato");
      setCell(lato);
      RoundedPanel latoEsterno = new RoundedPanel("Esterno");
      setCell(latoEsterno);
      RoundedPanel latoInterno = new RoundedPanel("Interno fuori terra");
      setCell(latoInterno);
      RoundedPanel latoInterrato = new RoundedPanel("Interno interrato");
      setCell(latoInterrato);
      RoundedPanel noteLabel = new RoundedPanel("Note");
      setCell(noteLabel);
      RoundedPanel elIntonaco = new RoundedPanel("Tipologia intonaco");
      setCell(elIntonaco);
      RoundedPanel tipoPitture = new RoundedPanel("Tipologia pitture");
      setCell(tipoPitture);
      RoundedPanel anno = new RoundedPanel("Anno");
      setCell(anno);
      anno.setPreferredSize(new Dimension(120, 45));
      RoundedPanel piano = new RoundedPanel("Piano");
      setCell(piano);
      piano.setPreferredSize(new Dimension(100, 45));

      setCell();
      altroActionListener();
      addMouse();

      add(lato, "cell 0 0 , gapleft 10");
      add(elIntonaco, "cell 1 0");
      add(tipoPitture, "cell 2 0");
      add(anno, "cell 3 0");
      add(piano, "cell 4 0");
      add(latoEsterno, "cell 0 1, gapleft 10");
      add(intonacoEsterno, "cell 1 1");
      add(pitturaEsterno, "cell 2 1");
      add(annoLEst, "cell 3 1");
      add(latoIntonaco, "cell 0 4 5 1 , wrap");
      add(noteLabel);
      add(note, "span");

   }

   public void addMouse() {
      MouseAdapter mlA1 = new MouseAdapter() {
         public void mousePressed(MouseEvent e) {
            if (e.getButton() == MouseEvent.BUTTON3) {
               remove(otherIntonEst);
               intonacoEsterno.setSelectedIndex(0);
               add(intonacoEsterno, "cell 1 1");
               modifyIntonEst = false;
               paintAll(getGraphics());
               // st.refresh();
            }
         }
      };
      otherIntonEst.addMouseListener(mlA1);

      MouseAdapter mlA2 = new MouseAdapter() {
         public void mousePressed(MouseEvent e) {
            if (e.getButton() == MouseEvent.BUTTON3) {
               remove(otherPittEst);
               pitturaEsterno.setSelectedIndex(0);
               add(pitturaEsterno, "cell 2 1");
               modifyPittEst = false;

               paintAll(getGraphics());
            }
         }
      };
      otherPittEst.addMouseListener(mlA2);

   }

   public void setCell() {
      annoLEst.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
      note.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
      otherIntonEst.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
      otherPittEst.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));

      intonacoEsterno.setFont(new Font("Arial", Font.PLAIN, 16));
      pitturaEsterno.setFont(new Font("Arial", Font.PLAIN, 16));
      annoLEst.setFont(new Font("Arial", Font.PLAIN, 16));
      note.setFont(new Font("Arial", Font.PLAIN, 16));
      otherIntonEst.setFont(new Font("Arial", Font.PLAIN, 16));
      otherPittEst.setFont(new Font("Arial", Font.PLAIN, 16));

      intonacoEsterno.setPreferredSize(new Dimension(210, 45));
      pitturaEsterno.setPreferredSize(new Dimension(210, 45));
      annoLEst.setPreferredSize(new Dimension(120, 45));
      note.setPreferredSize(new Dimension(553, 45));
      otherIntonEst.setPreferredSize(new Dimension(210, 45));
      otherPittEst.setPreferredSize(new Dimension(210, 45));

      annoLEst.setOpaque(false);
      note.setOpaque(false);
      otherIntonEst.setOpaque(false);
      otherPittEst.setOpaque(false);

   }

   public void setCell(JComponent c) {
      // c.setBorder(new OvalBorder());
      c.setFont(new Font("Arial", Font.PLAIN, 16));
      c.setPreferredSize(new Dimension(210, 45));
      c.setOpaque(false);
      // c.setBackground(new Color(238, 131, 107));
   }

   public void altroActionListener() {
      intonacoEsterno.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent arg0) {
            if (((String) intonacoEsterno.getSelectedItem()).equalsIgnoreCase("altro")) {
               remove(intonacoEsterno);
               add(otherIntonEst, "cell 1 1");
               modifyIntonEst = true;
               st.refresh();
            }
         }
      });

      pitturaEsterno.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent arg0) {
            if (((String) pitturaEsterno.getSelectedItem()).equalsIgnoreCase("altro")) {
               remove(pitturaEsterno);
               add(otherPittEst, "cell 2 1");
               modifyPittEst = true;
               st.refresh();
            }
         }
      });

   }

   public String print() {
      String print = "\n#";
      if (modifyIntonEst)
         print = print + otherIntonEst.getText() + "\n#";
      else
         print = print + intonacoEsterno.getSelectedIndex() + "\n#";

      if (modifyPittEst)
         print = print + otherPittEst.getText() + "\n#";
      else
         print = print + pitturaEsterno.getSelectedIndex() + "\n#";
      print = print + annoLEst.getText();

      print = print + latoIntonaco.print();

      print = print + "\n#" + note.getText();
      return print;
   }

   public void setLatoIntonaco(String data) {
      latoIntonaco.setElement(data);
   }

   public void setElement(String data) {
      Scanner c = new Scanner(data);
      c.nextLine();
      String tmp = c.nextLine();
      try {
         intonacoEsterno.setSelectedIndex(Integer.parseInt(tmp));
      } catch (NumberFormatException e) {
         otherIntonEst.setText(tmp);
         remove(intonacoEsterno);
         add(otherIntonEst, "cell 1 1");
         modifyIntonEst = true;
      }
      tmp = c.nextLine();
      try {
         pitturaEsterno.setSelectedIndex(Integer.parseInt(tmp));
      } catch (NumberFormatException e) {
         otherPittEst.setText(tmp);
         remove(pitturaEsterno);
         add(otherPittEst, "cell 2 1");
         modifyPittEst = true;
      }

      try {
         annoLEst.setText(c.nextLine());
      } catch (Exception e) {
         annoLEst.setText("");
      }
   }

   public void setNote(String data) {
      note.setText(data);
   }

   public Document PDF(Document doc) {
      PdfPTable table = new PdfPTable(5);
      table.setKeepTogether(true);
      table.setSpacingBefore(20);
      int[] size = { 60, 60, 60, 15, 15 };
      try {
         table.setWidths(size);
         table.setWidthPercentage(100);
      } catch (DocumentException e1) {
         // TODO Auto-generated catch block
         e1.printStackTrace();
      }
      Phrase testo = new Phrase("3.3 Tipo dell'intonaco, della pittura e periodo", FontFactory.getFont("Times", 14, 1));
      PdfPCell cos = new PdfPCell(testo);
      cos.setHorizontalAlignment(Element.ALIGN_LEFT);
      cos.setFixedHeight(30.0F);
      cos.setColspan(5);
      cos.setBorder(0);
      table.addCell(cos);

      testo = new Phrase("Lato", FontFactory.getFont(FontFactory.TIMES, 11, Font.BOLD));
      cos = new PdfPCell(testo);
      cos.setHorizontalAlignment(Element.ALIGN_CENTER);
      cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
      cos.setFixedHeight(30);
      table.addCell(cos);

      testo = new Phrase("Tipologia intonaco", FontFactory.getFont(FontFactory.TIMES, 11, Font.BOLD));
      cos = new PdfPCell(testo);
      cos.setHorizontalAlignment(Element.ALIGN_CENTER);
      cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
      cos.setFixedHeight(30);
      table.addCell(cos);

      testo = new Phrase("Tipologia pittura", FontFactory.getFont(FontFactory.TIMES, 11, Font.BOLD));
      cos = new PdfPCell(testo);
      cos.setHorizontalAlignment(Element.ALIGN_CENTER);
      cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
      cos.setFixedHeight(30);
      table.addCell(cos);

      testo = new Phrase("Anno", FontFactory.getFont(FontFactory.TIMES, 11, Font.BOLD));
      cos = new PdfPCell(testo);
      cos.setHorizontalAlignment(Element.ALIGN_CENTER);
      cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
      cos.setFixedHeight(30);
      table.addCell(cos);

      testo = new Phrase("Piano", FontFactory.getFont(FontFactory.TIMES, 11, Font.BOLD));
      cos = new PdfPCell(testo);
      cos.setHorizontalAlignment(Element.ALIGN_CENTER);
      cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
      cos.setFixedHeight(30);
      table.addCell(cos);

      // line 1 esterno
      testo = new Phrase("Esterno", FontFactory.getFont(FontFactory.TIMES, 11, Font.BOLD));
      cos = new PdfPCell(testo);
      cos.setHorizontalAlignment(Element.ALIGN_CENTER);
      cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
      cos.setFixedHeight(26);
      table.addCell(cos);

      if (modifyIntonEst)
         testo = new Phrase(otherIntonEst.getText(), FontFactory.getFont(FontFactory.TIMES, 11));
      else

         testo = new Phrase(((String) intonacoEsterno.getSelectedItem()).replace("<html>", "").replace("<br>", " ").replace("</html>", ""),
               FontFactory.getFont(FontFactory.TIMES, 11));
      cos = new PdfPCell(testo);
      cos.setHorizontalAlignment(Element.ALIGN_CENTER);
      cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
      cos.setFixedHeight(26);
      table.addCell(cos);

      if (modifyPittEst)
         testo = new Phrase(otherPittEst.getText(), FontFactory.getFont(FontFactory.TIMES, 11));
      else
         testo = new Phrase(((String) pitturaEsterno.getSelectedItem()).replace("<html>", "").replace("<br>", " ").replace("</html>", ""),
               FontFactory.getFont(FontFactory.TIMES, 11));
      cos = new PdfPCell(testo);
      cos.setHorizontalAlignment(Element.ALIGN_CENTER);
      cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
      cos.setFixedHeight(26);
      table.addCell(cos);

      testo = new Phrase(annoLEst.getText(), FontFactory.getFont(FontFactory.TIMES, 11));
      cos = new PdfPCell(testo);
      cos.setHorizontalAlignment(Element.ALIGN_CENTER);
      cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
      cos.setFixedHeight(26);
      table.addCell(cos);

      testo = new Phrase("");
      cos = new PdfPCell(testo);
      cos.setFixedHeight(26);
      table.addCell(cos);

      // note
      if (!note.getText().equals("")) {
         // lato intonaco
         latoIntonaco.pdf(table);

         testo = new Phrase("Note", FontFactory.getFont(FontFactory.TIMES, 12, Font.BOLD));
         cos = new PdfPCell(testo);
         cos.setBorder(0);
         cos.setHorizontalAlignment(Element.ALIGN_LEFT);
         cos.setColspan(5);
         table.addCell(cos);

         testo = new Phrase(note.getText(), FontFactory.getFont(FontFactory.TIMES, 12));
         cos = new PdfPCell(testo);
         cos.setBorder(0);
         cos.setHorizontalAlignment(Element.ALIGN_LEFT);
         cos.setColspan(5);
         table.addCell(cos);

         try {
            table.setSpacingBefore(10);
            doc.add(table);
         } catch (DocumentException e) {
            e.printStackTrace();
         }

      } else {
         try {
            // lato intonaco
            latoIntonaco.pdf(table);
            doc.add(table);
         } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
      }
      return doc;
   }

}
